package main.social.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;

import main.social.dao.AccountDao;
import main.social.entities.Accounts;

public class AccountDaoImpl extends AbstractGenericDao<Accounts, Long> implements AccountDao {

	@SuppressWarnings("rawtypes")
	@Override
	public Accounts findAccountById(long id) {
		Accounts result = null;
		try{
			List list=getHibernateTemplate().find("from Accounts where id=?",id);
			if((list != null) &&(list.size() > 0)){
				result = (Accounts) list.get(0);
			}
		}catch(DataAccessException dae){
			logger.info("DataAccessException: " + dae.getMessage());
		}
        return result;
	}

	@Override
	public boolean insertAccount(Accounts account) {
		try{
			getHibernateTemplate().save(account);
		}catch(DataAccessException dae){
			logger.info("DataAccessException: " + dae.getMessage());
			return false;
		}
		return true;
	}

	@Override
	public boolean updateAccount(Accounts account) {
		try{
			getHibernateTemplate().update(account);
		}catch(DataAccessException dae){
			logger.info("DataAccessException: " + dae.getMessage());
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Accounts> findAllAccounts() {
		List<Accounts> result = null;
		try{
			result = (List<Accounts>)getHibernateTemplate().loadAll(getPersistentClass());
		}catch(DataAccessException dae){
			logger.info("DataAccessException: " + dae.getMessage());
		}
		return result;
	}

	@Override
	public boolean removeAccount(Accounts account) {
		try{
			getHibernateTemplate().delete(account);
		}catch(DataAccessException dae){
			logger.info("DataAccessException: " + dae.getMessage());
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Accounts findAccountByUsername(final String username) {
		Accounts result = null;
		List<Accounts> listAccounts = (List<Accounts>) getHibernateTemplate().executeFind(new HibernateCallback() {
			@SuppressWarnings("rawtypes")
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
					Criteria crit = session.createCriteria(getPersistentClass());
					crit.add(Restrictions.eq("username", username));
				return crit.list();
			}
		});
		if ((listAccounts != null) && (listAccounts.size() > 0)){
			result = listAccounts.get(0);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Accounts findAccountByEmail(final String email) {
		Accounts result = null;
		List<Accounts> listAccounts = (List<Accounts>) getHibernateTemplate().executeFind(new HibernateCallback() {
			@SuppressWarnings("rawtypes")
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
					Criteria crit = session.createCriteria(getPersistentClass());
					crit.add(Restrictions.eq("email", email));
				return crit.list();
			}
		});
		if ((listAccounts != null) && (listAccounts.size() > 0)){
			result = listAccounts.get(0);
		}
		return result;
	}

	@Override
	public Accounts newEntityInstance() {
		Accounts result = new Accounts();
		result.setIsactive(false);
		return result;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean Login(final String username, final String password) {
		boolean result = false;
		List list = (List) getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria crit = session.createCriteria(getPersistentClass());
				crit.add(Restrictions.eq("username", username)).
					add(Restrictions.eq("password", password)).
					add(Restrictions.eq("isactive", true));
				return crit.list();
			}
		});
		if((list != null) &&(list.size() > 0)){
			result = true;
		}
		return result;
	}
}
